package zhongbo.java.air.monitor.we;

import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@ServerEndpoint("/websocket/alert")
@Component
public class AlertZoneWebSocket {
    public static Map<String, AlertZoneWebSocket> WeAirs = new ConcurrentHashMap<>();
    public Session session;

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        WeAirs.put(session.getId(), this);
    }

    @OnMessage
    public void onMessage(String message) {
        System.out.println("来自客户端的消息:" + message);
    }

    @OnError
    public void onError(Throwable error) {
        System.out.println("发生错误：" + error.getMessage());
    }

    @OnClose
    public void onClose(Session session) {
        WeAirs.remove(session.getId());
        System.out.println("连接关闭");
    }
}
